package com.myapplication.phonemanage;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.List;

import static com.myapplication.phonemanage.MainActivity.TAG;

/**
 * Created by admin on 2018/1/25.
 */

public class IntentUtils {

    /**
     * 判断服务是否运行
     */
    private static boolean isServiceRunning(Context context,final String className) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> info = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (info == null || info.size() == 0) return false;
        for (ActivityManager.RunningServiceInfo aInfo : info) {
            Log.e(TAG, "isServiceRunning: ___" +aInfo.service.getClassName());
            if (className.equals(aInfo.service.getClassName())) return true;
        }
        return false;
    }

    public static void staTtSer(Context context){
        if (isServiceRunning(context,"com.example.shoewann.autoexecutetask.service.AutoTaskTTiaoService")){
            Log.e("服务正在运行 不会重复启动第二次","return");
            return;
        }
        Log.e("启动服务","return");
        ShellUtils.execCommand("am startservice -n com.example.shoewann.autoexecutetask/com.example.shoewann.autoexecutetask.service.AutoTaskTTiaoService", true, true);
    }



    public static void staKeep(Context context){
        if (isServiceRunning(context,"com.myapplication.phonemanage.KeepAliveService")){
            Log.e("服务正在运行 不会重复启动第二次","return");
            return;
        }


        Log.e("启动服务","return");
        Intent starter = new Intent(context, KeepAliveService.class);
//        starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(starter);
    }
}
